import { GCtrl } from "../../../Core/GCtrl";
import GViewBase from "../../../Core/GView/GViewBase";
import { CMsg } from "../../Common/Define";
import { Res } from "../../Common/UIResources";
import RecuitCardItem from "./RecuitCardItem";
const { ccclass, property, menu, executeInEditMode } = cc._decorator;
@ccclass @menu('View/Home/RecruitCardCtrl')

export default class RecruitCardCtrl extends GViewBase {
    @property({ type: cc.Node, tooltip: "招募单张抽卡" }) onceCard: cc.Node = null;
    @property({ type: cc.Node, tooltip: "招募10张抽卡" }) tenCard: cc.Node = null;
    @property({ type: cc.Node, tooltip: "招募10张抽卡位移位置" }) tenCardPos: cc.Node = null;
    onGLoad() {

    }

    onGStart(recruitCardCtrl: recruitCardCtrl) {
        this.win.maskNode.opacity = 215;
        console.log('recruitCardCtrl', recruitCardCtrl);
        if (recruitCardCtrl.recruitNum == 1) {
            if (this.onceCard.children.length > 0) {
                console.log('recruitCardCtrl存在卡牌');
                let comp = this.onceCard.getChildByName("RecuitCardItem").getComponent(RecuitCardItem);
                comp.init(0, recruitCardCtrl.award[0], recruitCardCtrl.cb);

            } else {
                console.log('recruitCardCtrl不存在卡牌');
                this.assetImpl.prefab(Res.prefab.vw.home.recuitCardItem, (prefab) => {
                    let comp = this.assetImpl.addGChild<RecuitCardItem>(prefab);
                    comp.node.parent = this.onceCard;
                    comp.node.scale = 1.3;
                    comp.init(0, recruitCardCtrl.award[0], recruitCardCtrl.cb);
                    GCtrl.ES.emit(CMsg.client.home.onRecruit);
                })
            }
        }

        if (recruitCardCtrl.recruitNum == 10) {
            if (this.tenCard.children.length > 0) {
                console.log('recruitCardCtrl存在卡牌');
            } else {
                console.log('recruitCardCtrl不存在卡牌10');
                this.assetImpl.prefab(Res.prefab.vw.home.recuitCardItem, (prefab) => {
                    for (let i = 0; i < recruitCardCtrl.recruitNum; i++) {
                        let comp = this.assetImpl.addGChild<RecuitCardItem>(prefab);
                        comp.node.parent = this.tenCardPos.children[i];
                        comp.node.position.x = 0;
                        comp.node.position.y = 0;
                        if (i == recruitCardCtrl.recruitNum - 1) {
                            comp.init(0, recruitCardCtrl.award[i], recruitCardCtrl.cb);
                        } else {
                            comp.init(0, recruitCardCtrl.award[i]);
                        }

                    }
                })
            }
        }
    }
}